﻿/*
 * Created by SharpDevelop.
 * User: alaa
 * Date: 2/6/2013
 * Time: 1:42 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Windows_System_Snapshot
{
	/// <summary>
	/// Description of EmbeddedResource.
	/// </summary>
	public class EmbeddedResource
	{
		public EmbeddedResource()
		{
		}
		
		//Recreate all executable resources
	    public static bool RecreateEmbeddedResource(string resName, string targetDir) 
	    {
	    	try
	    	{
		        // Get Current Assembly refrence
		        Assembly currentAssembly = Assembly.GetExecutingAssembly();
		        // Get all imbedded resources
		        string[] arrResources = currentAssembly.GetManifestResourceNames();
				foreach (string resourceName in arrResources) 
				{
                	if (resourceName.Contains(resName)) 
                	{
				        //Name of the file saved on disk
				        string saveAsName = targetDir + @"\" + resourceName; 
				        saveAsName = Regex.Replace(saveAsName, "Windows_System_Snapshot.", "");
				        FileInfo fileInfoOutputFile = new FileInfo(saveAsName);
				   		//CHECK IF FILE EXISTS AND DO SOMETHING DEPENDING ON YOUR NEEDS
				        if (fileInfoOutputFile.Exists) 
				        {
				            return true;
				        }
				        //OPEN NEWLY CREATING FILE FOR WRITTING
				        FileStream streamToOutputFile = fileInfoOutputFile.OpenWrite();
				        //GET THE STREAM TO THE RESOURCES
				        Stream streamToResourceFile = currentAssembly.GetManifestResourceStream(resourceName);
				        // SAVE TO DISK
				        const int size = 4096;
				        byte[] bytes = new byte[4096];
				        int numBytes;
				        while ((numBytes = streamToResourceFile.Read(bytes, 0, size)) > 0) 
				        {
				            streamToOutputFile.Write(bytes, 0, numBytes);
				        }
				        streamToOutputFile.Close();
				        streamToResourceFile.Close();
				        return true;
                	}
				}
				return false;
	    	}
	    	catch 
	    	{
	    		return false;
	    	}
	    }
	    //-------------------------------------------------------------------------------------------
	    //
	    
	    public static void DeleteFolderRec(string targetDir)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(targetDir);
                /// delete all files in the folder
                foreach (FileInfo file in di.GetFiles())
                {
                    try
                    {
                        file.Delete();
                    }
                    catch { }
                }
                /// recursive call for all the sub-folders
                foreach (DirectoryInfo subfolder in di.GetDirectories())
                {
                    DeleteFolderRec(subfolder.FullName);
                }
                /// delete the current folder
                di.Delete();
            }
            catch { }
        }
        //-------------------------------------------------------------------------------------------
	    //
	}
}
